package net.emforge.updatestatus.dao;

import java.util.List;

import javax.persistence.EntityManagerFactory;

import net.emforge.updatestatus.entity.Usuario;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.stereotype.Repository;

/** DAO for working with users
 * 
 * @author akakunin
 *
 */
@Repository
public class UserDao extends JpaDaoSupport {
	@Autowired
	public UserDao(EntityManagerFactory entityManagerFactory) {
		setEntityManagerFactory(entityManagerFactory);
	}
	
	@SuppressWarnings("unchecked")
	public Usuario findByName(String userName) {
		List<Usuario> usuarios = getJpaTemplate().findByNamedQuery("Usuario.findByName", userName);
		
		if (usuarios.size() > 0) {
			return usuarios.get(0);
		} else {
			return null;
		}
	}

	/** Persist user object
	 * 
	 * @param usuario
	 */
	public void saveOrUpate(Usuario usuario) {
		getJpaTemplate().persist(usuario);
	}

}
